package com.sk.restree.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.ant.ListTask;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.sk.restree.model.dto.BankDTO;
import com.sk.restree.model.dto.CardDTO;
import com.sk.restree.model.dto.MemberDTO;
import com.sk.restree.model.ibatis.SqlMapFactory;

public class BankListDAO {
	private static BankListDAO instance;
	private List<BankDTO> banks;
	private SqlMapClient sqlMap;

	static {
		instance = new BankListDAO();
	}

	private BankListDAO() {
		banks = new ArrayList<BankDTO>();
		init();
	}

	private void init() {
		sqlMap = SqlMapFactory.getSqlMapInstance();
		setBankList(sqlMap);
	}

	public static BankListDAO getIntance() {
		return instance;
	}

	@SuppressWarnings("unchecked")
	public List<BankDTO> setBankList(SqlMapClient sqlMap) {
		try {
			banks = sqlMap.queryForList("selectBankList", null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return banks;
	}

	public List<BankDTO> getBanklist() {
		if(banks.size()==0)
			init();
		return banks;
	}

	public String getBankName(String string) {
		if(banks.size()==0)
			init();
		for (BankDTO bank : banks) {
			if (string.contains(bank.getBname()))
				return bank.getBname();
		}
		return null;
	}

	public BankDTO getBank(String bankName) {
		if(banks.size()==0)
			init();
		for (BankDTO bank : banks) {
			if (bank.getBname().equals(bankName))
				return bank;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<CardDTO> selectCardAsBname(String bname) {
		String bank_id = getBank(getBankName(bname)).getBank_id();
		List<CardDTO> cardlist = null;
		try {
			cardlist = (List<CardDTO>) sqlMap.queryForList("selectcard", bank_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cardlist;
	}
	
	@SuppressWarnings("unchecked")
	public CardDTO selectCardAsCardName(String cardName) {
		CardDTO card = null;
		try {
			card = (CardDTO) sqlMap.queryForObject("selectCardAsCardName", cardName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return card;
	}

	
}
